<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class AcsController extends Controller
{
	// 地区管理----------------------------------------------------------------------------
    public function getAreaList(){
    	$arealist = DB::table('areas')->paginate(10);
    	return view('admin/arealist',['areas'=>$arealist]);
    }

    public function getAreaEdit($aid){
    	$area = DB::table('areas')->where('aid',$aid)->first();
    	//dd($area->title);
    	return view('admin/editarea',['area'=>$area]);
    }

    public function postAreaEdit($aid){
    	//dd($_POST);
    	$areaedit = DB::table('areas')->where('aid',$aid)->update([
    		'title'=>$_POST['title'],
    		'area'=>$_POST['area']
    	]);

    	return $areaedit ? redirect('arealist') : '修改失败,请稍后重试!';
    }

    public function getAreaDel($aid){
    	return DB::table('areas')->where('aid',$aid)->delete() ? redirect('arealist') : '删除失败,请稍后重试!';
    }

    // 店铺管理----------------------------------------------------------------------------
    public function getShopList(){
    	$shoplist = DB::table('stores')->paginate(10);
    	return view('admin/shoplist',['stores'=>$shoplist]);
    }

    public function getShopAdd(){
    	return view('admin/addshop');
    }

    public function postShopAdd(){
    	//dd($_POST);
    	$in_stores = DB::insert('insert into stores (sname, lev, status, qsjg) values (?,?,?,?)',
        [$_POST['sname'], $_POST['lev'], $_POST['status'], $_POST['qsjg']] );
    	return $in_stores ? redirect('shoplist') : '添加店铺失败,请稍后重试!';
    }

    public function getShopEdit($sid){
    	$stores = DB::table('stores')->where('sid',$sid)->first();
    	//dd($stores);
    	return view('admin/editshop',['stores'=>$stores]);
    }

    public function postShopEdit($sid){
    	//dd($_POST['lev']);
    	$up_stores = DB::table('stores')->where('sid',$sid)->update([
    		'sname'=>$_POST['sname'],
    		'lev'=>$_POST['lev'],
    		'status'=>$_POST['status'],
    		'qsjg'=>$_POST['qsjg']
    	]);
    	return $up_stores ? redirect('shoplist') : '修改失败,请稍后重试!';
    }

    public function getShopDel($sid){
    	return DB::table('stores')->where('sid',$sid)->delete() ? redirect('shoplist') : '删除失败,请稍后重试!';
    }

    // 分类管理----------------------------------------------------------------------------
    public function getCatList(){
    	$catlist = DB::table('category')
    		   ->join('stores','stores.sid','=','category.sid')
    		   ->select('stores.*','category.*')
    		   ->paginate(10);
    	return view('admin/catlist',[    		
    		'catlist'=>$catlist
    	]);
    }

    public function getCatAdd(){
    	$stores = DB::table('stores')->paginate(10);
    	return view('admin/addcat',['stores'=>$stores]);
    }

    public function postCatAdd(){
    	//dd($_POST);
    	$in_category = DB::insert('insert into category (lname, sid) values (?,?)',
        [$_POST['lname'], $_POST['sid']] );
    	return $in_category ? redirect('catlist') : '添加店铺失败,请稍后重试!';
    }

    public function getCatEdit($lid){
    	$cat = DB::table('category')->where('lid',$lid)->first();
    	$store = DB::table('stores')->where('sid',$cat->sid)->first();
    	//dd($cat);
    	return view('admin.editcat',[
    		'cat'=>$cat,
    		'store'=>$store
    	]);
    }

    public function postCatEdit($lid){
    	//dd($_POST);
    	$up_category = DB::table('category')->where('lid',$lid)->update([
    		'sid'=>$_POST['sid'],
    		'lname'=>$_POST['lname']
    	]);
    	return $up_category ? redirect('catlist') : '修改失败,请稍后重试!';
    }

    public function getCatDel($lid){
    	return DB::table('category')->where('lid',$lid)->delete() ? redirect('catlist') : '删除失败,请稍后重试!';
    }
}
